package cn.com.ddapp.ddm.ui.mine;

import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TimeCount;
import cn.com.ddapp.ddm.util.ToolFor9Ge;

//修改支付密码
public class AlterPwdActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.et_phone_alter)
    EditText etPhoneAlter;
    @BindView(R.id.get_verify_code1)
    TextView getVerifyCode1;
    @BindView(R.id.et_verify_alter)
    EditText etVerifyAlter;
    @BindView(R.id.et_old_pwd)
    EditText etOldPwd;
    @BindView(R.id.et_new_pwd)
    EditText etNewPwd;
    @BindView(R.id.et_again_pwd)
    EditText etAgainPwd;
    @BindView(R.id.commit_alter)
    Button commitAlter;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;

    OkUtilCallback callback1,callback2;
    TimeCount timeCount;

    @Override
    public void initView() {
        titletext.setText("修改支付密码");
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_alter_pwd;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }
    @OnClick({R.id.commit_alter,R.id.back,R.id.get_verify_code1})
    public void onclick(View view){
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.commit_alter:
                String userId = SPUtils.getStringData(this, SPUtils.USERID);
                String oldPwd = etOldPwd.getText().toString();
                String pwd = etNewPwd.getText().toString();
                String confirmPwd = etAgainPwd.getText().toString();
                String randCode = etVerifyAlter.getText().toString();
                if ("".equals(oldPwd)) {
                    BaseApplication.getIntstance().showToast("请输原密码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                } else if ("".equals(pwd)) {
                    BaseApplication.getIntstance().showToast("请输入新密码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                }else if ("".equals(confirmPwd)) {
                    BaseApplication.getIntstance().showToast("请确认密码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                }else if ("".equals(randCode)) {
                    BaseApplication.getIntstance().showToast("请输入验证码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                } else {
                    if (!NetUtils.isConnected(this)) {
                        Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
                    } else {
                        mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                        callback1 = new OkUtilCallback(mEmptyLayout) {
                            @Override
                            public void onMyResponse(String response, int id) {
                                Gson gson = new Gson();
                                Entity entity = gson.fromJson(response, Entity.class);
                                if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                    BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                    BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                    BaseApplication.getIntstance().showToast("绑定成功", Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                    finish();
                                }
                            }
                        };
                        /*修改支付密码
                    userId [Y] 会员ID
                    oldPwd [Y] 旧密码
                    pwd string[Y] 密码
                    confirmPwd string[Y] 确认密码
                    randCode string[Y]*/
                        OKUtilAPI.post(OKUtilAPI.updatePayPwd, "userId=" + userId + "&oldPwd=" + oldPwd + "&pwd=" + pwd+"&confirmPwd=" + confirmPwd + "&randCode=" + randCode, callback1);
                    }
                }
                break;
            case R.id.get_verify_code1:
                String phone = etPhoneAlter.getText().toString();
                if (!ToolFor9Ge.isPhoneNO(phone)){
                    BaseApplication.getIntstance().showToast("请输入正确的电话号码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                } else {
                    callback2 = new OkUtilCallback() {
                        @Override
                        public void onMyResponse(String response, int id) {
                            Gson gson = new Gson();
                            Entity entity = gson.fromJson(response, Entity.class);
                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                            } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast("验证码已发送", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                timeCount.start();
                            }
                        }
                    };
                    OKUtilAPI.post(OKUtilAPI.sendVerifyCode, "key=" + etPhoneAlter.getText().toString(), callback2);
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        timeCount = new TimeCount(60000, 1000, getVerifyCode1);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timeCount.cancel();
    }
}
